<template>
  <item-add text="返回" url="/results"></item-add>
  <div class="form utils-flex-Justcenter">
    <el-form :model="resultFrom" label-width="100px" class="demo-ruleForm">
      <el-form-item label="成果名称">
        <el-input v-model="resultFrom.name"></el-input>
      </el-form-item>
      <el-form-item label="参与人员">
        <el-input v-model="resultFrom.stuname"></el-input>
      </el-form-item>
      <el-form-item label="添加类型">
<!--        <el-select v-model="resultFrom.type" placeholder="请选择" @change="changeType">-->
<!--          <el-option-->
<!--              v-for="item in options"-->
<!--              :key="item.value"-->
<!--              :label="item.label"-->
<!--              :value="item.value">-->
<!--          </el-option>-->
<!--        </el-select>-->
        <el-radio-group v-model="resultFrom.type" @change="changeType">
          <el-radio :label="item.value" v-for="item in options" :key="item.value">{{ item.label }}</el-radio>
<!--          <el-radio label="studentPro" >社会服务</el-radio>-->
<!--          <el-radio label="studentPaper" >专利论文</el-radio>-->
        </el-radio-group>
      </el-form-item>
      <el-form-item label="成果照片">
        <img-update :url="resultFrom.imagelogo" @successUpdate="success_"></img-update>
      </el-form-item>
      <el-form-item>
        <el-button @click="addResult" type="primary">{{ id === null ? '添加':'修改'}}</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {inject, onMounted, reactive, toRefs, ref, unref} from "vue";
import {ElMessage} from 'element-plus';
import {useRouter, useRoute} from 'vue-router';

import ItemAdd from "../../../components/itemAdd.vue";

export default {
  name: "index",
  components: {ItemAdd},
  setup() {
    const data = reactive({
      resultFrom: {
        type: "studentCom",
        name: "",
        stuname: "",
        imagelogo: ""
      },
      id: null,
      isEdit: false,
      options : [{
        value: 'studentCom',
        label: '学生竞赛'
      }, {
        value: 'studentPro',
        label: '社会服务'
      }, {
        value: 'studentPaper',
        label: '专利论文'
      }]
    })
    const $http = inject("$http")
    const router = useRouter()
    const route = useRoute(); // 1
    onMounted(() => {
      console.log("测试")
      if (route.query.action) {
        data.isEdit = true
        const {
          type,
          name,
          stuname,
          imagelogo,
          id
        } = JSON.parse(route.query.action)
        data.resultFrom = {
          type,
          name,
          stuname,
          imagelogo,
        }
        data.id = id
        console.log(data.resultFrom)
      }
    })
    const changeType = (eve) => {
      console.log(eve)
      data.resultFrom.type = eve
    }
    const success_ = (url) => {
      data.resultFrom.imagelogo = url
    }
    const addResult = async () => {
      if (!data.resultFrom.imagelogo) {
        ElMessage.error("请上传图片")
        return
      }
      let res = null
      if(data.id === null) {
        res = await $http.unityInfo.createAchievement(data.resultFrom)
      } else {
        res = await $http.unityInfo.editAchievement(data.id,data.resultFrom)
      }
      if (res.code === 1) {
        ElMessage.success(res.msg)
        router.push({
          path: '/results'
        })
      } else {
        ElMessage.error(res.msg)
      }
    }
    return {
      ...toRefs(data),
      changeType,
      success_,
      addResult
    }
  }
}
</script>

<style scoped lang="less">
.demo-ruleForm {
  width: 500px;
  margin-top: 30px;
}

</style>